import request from '@/utils/request'

import { getToken } from '@/utils/auth'

const x_www_form_urlencoded = function(data) {
  console.log('---------------------------------------------')
  let ret = ''
  for (const it in data) {
    ret += encodeURI(it) + '=' + encodeURI(data[it]) + '&'
  }
  if (ret) ret = ret.substring(0, ret.length - 1)
  return ret
}

export function getWorkerList(param) {
  // query.token = getToken()
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + getToken() + '/workers',
    method: 'get',
    params: param
  })
}

export function getWorkInfoById(param) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/workers/' + param.workerId + '',
    method: 'get',
    params: param
  })
}

export function getCategoryGroups() {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + getToken() + '/categoryGroups',
    method: 'get'
  })
}

export function getCategoryGroupsById(workerId) {
  console.warn('workerId' + workerId)
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/categoryGroups/' + workerId + '?workerId=' + workerId + '',
    method: 'get'
  })
}

export function provinces() {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/provinces',
    method: 'get'
  })
}

export function citiesByPid(pid) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + pid + '/cities?pid=' + pid + '',
    method: 'get'
  })
}

export function areasByCid(cid) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + cid + '/areas?cid=' + cid + '',
    method: 'get'
  })
}

export function streetsByaid(aid) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + aid + '/streets?aid=' + aid + '',
    method: 'get'
  })
}

export function serviceAreas(workerId) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/serviceAreas?workerId=' + workerId,
    method: 'get'
  })
}

export function saveWorker(data) {
  const transformRequest = [x_www_form_urlencoded]
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + getToken() + '/saveWorker',
    method: 'post',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
    data,
    transformRequest
  })
}

export function delWorker(workerId) {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + getToken() + '/delWorker?workerId=' + workerId + '',
    method: 'delete'
  })
}

export function getVisitUrl() {
  return request({
    url: '/wxh-site-rest/rest/webpoint/worker/' + getToken() + '/getVisitUrl',
    method: 'get'
  })
}

